package cc.plumgo.j2me.utils;

import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

public class CustomEntity {
	public static final int DIR_LEFT = 0;
	public static final int DIR_RIGHT = 1;
	public static final int DIR_NUM = 2;
	
	protected int direct = DIR_RIGHT;
	protected int posX;
	protected int posY;
	protected boolean visible = false;
	protected int state;
	protected int totalState;
	
	protected Sprite[] sprites;
	
	
	public int getRefPixelX() {
		return posX;
	}

	public int getRefPixelY() {
		return posY;
	}

	public void setPosition(int x, int y) {
		posX = x;
		posY = y;
	}
	
	public void updateAllPositions() {
		for (int i = 0; i < sprites.length; i++) {
			sprites[i].setPosition(posX, posY);
		}
	}
	
	public void setDirect(int direct) {
		if (direct < 0 || direct >= DIR_NUM) {
			return;
		}
		this.direct = direct;
	}
	
	public int getDirect() {
		return direct;
	}
	
	public void updateSpriteDirect(int spIndex) {
		updateSpriteDirect(spIndex, DIR_LEFT);
	}
	
	public void updateSpriteDirect(int spIndex, int mainDir) {
		int tmp = (direct == DIR_LEFT) ?  Sprite.TRANS_MIRROR : Sprite.TRANS_NONE;
		if ( mainDir != DIR_LEFT) {
			tmp = (direct == DIR_LEFT) ?  Sprite.TRANS_NONE : Sprite.TRANS_MIRROR;
		}
		sprites[spIndex].setTransform(tmp);
	}
	
	public boolean isVisible() {
		return visible;
	}
	
	public void setLayer(LayerManager layerMana) {
		for (int i = 0; i < sprites.length; i++) {
			layerMana.insert(sprites[i], 0);
		}
	}
	
	public int getState() {
		return state;
	}

	public void setState(int state) {
		if (state < 0 || state >totalState) {
			return;
		}
		this.state = state;
	}
}
